package com.nf.health.app.customview;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.nf.health.app.R;

/**
 * 中间有文本(3个)的圆形pb
 * 用在膳食计划中显示营养配比
 */
@SuppressLint("DrawAllocation")
public class TextProgressBarView extends View {

    //属性值
    private int mBackColor;
    private int mFrontColor;
    private float mCircleWidth;
    private String mtitle = "蔬菜";
    private String mUnit = "kcal";

    private int width;
    private int height;
    private float radius;
    private float centerX;
    private float centerY;
    private RectF oval;
    private int max = 100;
    private int progress = 0;
    private Paint circlePaint;
    private Paint arcPaint;
    private Paint textPaint;

    public TextProgressBarView(Context context) {
        this(context, null);
    }

    public TextProgressBarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TextProgressBarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TextProgressBarView, 0, defStyle);
        int count = a.getIndexCount();
        for (int i = 0; i < count; i++) {
            int attrType = a.getIndex(i);
            switch (attrType) {
                case R.styleable.TextProgressBarView_backColor:
                    mBackColor = a.getColor(attrType, Color.GRAY);
                    break;
                case R.styleable.TextProgressBarView_frontColor:
                    mFrontColor = a.getColor(attrType, Color.BLUE);
                    break;
                case R.styleable.TextProgressBarView_circleWidth:
                    mCircleWidth = a.getDimension(attrType, 8.0f);
                    break;
                case R.styleable.TextProgressBarView_center_progress:
                    progress = a.getInt(attrType, 0);
                    break;
                case R.styleable.TextProgressBarView_max_progress:
                    max = a.getInt(attrType, 100);
                    break;
                case R.styleable.TextProgressBarView_center_title:
                    mtitle = a.getString(attrType);
                    break;
                case R.styleable.TextProgressBarView_center_unit:
                    mUnit = a.getString(attrType);
                    break;
                default:
                    break;
            }
        }
        a.recycle();
        initPaint();
    }

    private void initPaint() {
        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(mBackColor);
        circlePaint.setStrokeWidth(mCircleWidth);
        circlePaint.setStyle(Paint.Style.STROKE);

        arcPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        arcPaint.setColor(mFrontColor);
        arcPaint.setStrokeWidth(mCircleWidth);
        arcPaint.setStrokeCap(Paint.Cap.ROUND);
        arcPaint.setStyle(Paint.Style.STROKE);

        textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setColor(Color.BLACK);
        textPaint.setStyle(Paint.Style.FILL);

        textPaint.setTextAlign(Paint.Align.CENTER);//设置校准(坐标x会代表text的中心横坐标所在,对y无用)
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        centerX = width / 2;
        centerY = height / 2;
        radius = Math.min(centerX, centerY) - mCircleWidth / 2;//取最最小值来限制范围
        textPaint.setTextSize(radius / 3);//根据半径设置字体大小,不同分辨率的屏幕同样大小的view获得的半径是不一样的,因为单位是px
        oval = new RectF(centerX - radius, centerY - radius, centerX + radius,
                centerY + radius);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //画背景,就是个360°的空心圆圈
        canvas.drawCircle(centerX, centerY, radius, circlePaint);
        //画进度,根据设置的progress 画一个弧线在背景上
        canvas.drawArc(oval, -90, 360 * (progress * 1.0f / (max * 1.0f)), false, arcPaint);//注意 4/5 要占据80%而不是0
        //画文字(标题,进度,单位)
        drawCustomText(canvas);
    }

    private void drawCustomText(Canvas canvas) {
        canvas.translate(centerX, centerY);//画布移动到中心,方便计算坐标
        //标题
        Rect rect1 = new Rect();
        textPaint.setColor(0xcc2D2D2D);
        textPaint.getTextBounds(mtitle, 0, mtitle.length(), rect1);
        canvas.drawText(mtitle, 0, -radius / 2 + (rect1.height() / 2 - rect1.bottom), textPaint);
        //进度
        String jindu = progress + "";
        Rect rect2 = new Rect();
        textPaint.setColor(0xff2E86F4);
        textPaint.getTextBounds(jindu + "", 0, jindu.length(), rect2);
        canvas.drawText(jindu + "", 0, 0 + (rect2.height() / 2 - rect2.bottom), textPaint);
        //单位
        Rect rect3 = new Rect();
        textPaint.setColor(Color.GRAY);
        textPaint.getTextBounds(mUnit, 0, mUnit.length(), rect3);
        canvas.drawText(mUnit, 0, radius / 2 + (rect3.height() / 2 - rect3.bottom), textPaint);
    }


    public void setProgress(int max, int target) {
        if (target > max || target < 0) {
            return;
        }
        this.max = max;
        progress = target;
        postInvalidate();
    }

    public void setCenterTitle(String title) {
        this.mtitle = title;
        postInvalidate();
    }
}
